package goroutinePool

import (
	"fmt"
	"math/rand"
	"time"
)


func ExampleRun() {
	//初始化线程池
	PoolObj = NewPool(10, 200, 10)
	//协程池开始工作
	go PoolObj.Run()
	//定时输出log
	go func() {
		for {
			time.Sleep(time.Millisecond * 100)
			fmt.Println("----------------------入口池",
				len(PoolObj.EntryChannel), "就绪池",
				len(PoolObj.JobsChannel), "----------------------")
		}
	}()

	//模拟耗时任务
	t1 := NewTask(func() error {
		fmt.Println("一个", rand.Intn(10)+1, "秒的任务加入进入口池")
		time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second)
		return nil
	})

	//模拟创建指定个数的耗时任务
	go func() {
		for i := 0; i < 10; i++ {
			time.Sleep(500 * time.Millisecond)
			PoolObj.EntryChannel <- t1
		}
	}()

	time.Sleep(3 * time.Second)
	fmt.Println("第二次任务开始")
	//模拟创建指定个数的耗时任务
	go func() {
		for i := 0; i < 30; i++ {
			time.Sleep(200 * time.Millisecond)
			PoolObj.EntryChannel <- t1
		}
	}()

	time.Sleep(7 * time.Second)
	fmt.Println("第三次任务开始")
	//模拟创建指定个数的耗时任务
	go func() {
		for i := 0; i < 10; i++ {
			time.Sleep(150 * time.Millisecond)
			PoolObj.EntryChannel <- t1
		}
		//如果外界不关闭EntryChannel,则该协程池将会一直监听
		close(PoolObj.EntryChannel)
	}()

	//等待wg完毕后结束方法。
	Wg.Wait()
}

